package cn.school.job.attendance;

import cn.school.attendance.dormitory.dao.DormitoryAttendanceMapper;
import cn.school.attendance.dormitory.entity.DormitoryAttendanceDo;
import cn.school.student.dao.StudentInfoDao;
import cn.school.student.entity.StudentInfo;
import cn.school.student.param.StudentInfoPageParam;
import cn.school.workflow.dao.StudentLeaveApplyDao;
import cn.school.workflow.dao.StudentLeaveSelfApplyDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: lx
 * @date: 2024/1/12 14:10
 * @description: 学生宿舍考勤数据自动插入
 */
@Slf4j
public class StudentAttendanceInsertJob extends QuartzJobBean {

    @Autowired
    DormitoryAttendanceMapper attendanceMapper;

    @Autowired
    StudentInfoDao studentInfoDao;

    @Autowired
    StudentLeaveSelfApplyDao studentLeaveSelfApplyDao;

    @Autowired
    StudentLeaveApplyDao studentLeaveApplyDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info("StudentAttendanceInsertJob ======>  每日考勤数据自动插入");
        int pageSize = studentInfoDao.queryCount(null);
        StudentInfoPageParam studentInfoPageParam = new StudentInfoPageParam();
        studentInfoPageParam.setPageNum(1);
        studentInfoPageParam.setPageSize(pageSize);
        studentInfoPageParam.setStart(0);
        List<StudentInfo> studentInfoList = studentInfoDao.queryLimit(studentInfoPageParam);

        Date targetDate = new Date();
        SimpleDateFormat ymdsdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat ymdhmssdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //查询当天所有请假(自提和代提)
        List<String> leaveStuList = studentLeaveApplyDao.getLeaveStuList(ymdhmssdf.format(targetDate));
        List<String> selfLeaveStuList = studentLeaveSelfApplyDao.getSelfLeaveStuList(ymdhmssdf.format(targetDate));
        List<Long> repeat = attendanceMapper.getListByAttendanceDate(ymdsdf.format(targetDate));

        int count = 0;
        int leaveStu = 0;
        List<DormitoryAttendanceDo> insertList = new ArrayList<>();
        for(StudentInfo student : studentInfoList){
            //判断学生是否有效
            if("0".equals(student.getStatus())){
                continue;
            }
            //判断是否为住宿生
            if(student.getLevelName() == null || student.getLevelName().equals("")){
                continue;
            }
            //学生表主键ID
            String idStr = String.valueOf(student.getId());
            //学生表student_id
            String studentId = student.getStudentId();
            //跳过已存在考勤记录的学生
            if(repeat.contains(student.getId())){
//            if(repeat.stream().anyMatch( item -> item == student.getId())){
                continue;
            }
            count ++ ;

            DormitoryAttendanceDo attendance = new DormitoryAttendanceDo();
            attendance.setStudentId(student.getId());
            if(leaveStuList.contains(idStr) || selfLeaveStuList.contains(studentId)){
                leaveStu ++;
                attendance.setStatus("2");
            }else {
                attendance.setStatus("0");
            }
            attendance.setTenantId(student.getTenantId());
            attendance.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(targetDate));
            attendance.setCreateBy("sysJob");
            attendance.setAttendanceDate(ymdsdf.format(targetDate));

            insertList.add(attendance);
        }
        log.info("StudentAttendanceInsertJob ===>  【" + ymdhmssdf.format(targetDate) + "】系统插入的考勤共有：" + count + "人");
        log.info("StudentAttendanceInsertJob ===>  【" + ymdhmssdf.format(targetDate) + "】请假学生共有：" + leaveStu + "人");

        List<List<DormitoryAttendanceDo>> partition = ListUtils.partition(insertList, 100);
        for (List<DormitoryAttendanceDo> item : partition) {
            attendanceMapper.insertBatch(item);
        }
    }
}
